package com.maiji.cloud.service.impl;

import com.maiji.cloud.config.MyRedisConfig;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.entities.shopingmall.GoodsCommentComment;
import com.maiji.cloud.mapper.GoodsCommentCommentMapper;
import com.maiji.cloud.microservice.UserInfoService;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.GoodsCommentCommentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.RedisUtil;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class GoodsCommentCommentServiceImpl extends ServiceImpl<GoodsCommentCommentMapper, GoodsCommentComment>
        implements GoodsCommentCommentService {


    public static final String REDISNAMESPACE = "goodsComment";
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public BaseResDto replyGoodsComment(String gcId, String content, String token) {
        GoodsCommentComment goodsCommentComment = new GoodsCommentComment()
                .setUuId(UUID_MD5.getUUID())
                .setCommentId(gcId)
                .setContent(content)
                .setCommentDate(new Date());
        if (StringUtil.isNotBlank(token)) {
            AppUser appUser = RedisUtil.getAppUserByRedis(redisTemplate, MyRedisConfig.REDISDATABASENO, token, userInfoService);
            goodsCommentComment.setUserId(appUser.getUuId())
                    .setSellerReply(false);
        } else goodsCommentComment.setSellerReply(true);
        if (! insert(goodsCommentComment)) return new BaseResDto(Status.ERROR);
        redisTemplate.delete(RedisUtil.getRedisKey(REDISNAMESPACE, gcId));
        return new BaseResDto(Status.SUCCESS);
    }

}
